package com.yanggeng.demo.beginnerguidedemo.widget;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;

import com.github.mikephil.charting.formatter.IFillFormatter;

/**
 * Created by yanggeng on 2017/8/24.
 */

public class ScrollHideBehavior extends CoordinatorLayout.Behavior<View> {
    private float viewY;
    private static final Interpolator interpolator = new FastOutSlowInInterpolator();
    private boolean isAnimate;

    public ScrollHideBehavior(Context context, AttributeSet attr) {
        super(context, attr);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {

        if (child.getVisibility() == View.VISIBLE && viewY == 0) {
            viewY = coordinatorLayout.getHeight() - child.getY();
        }
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {

        if (dy > 0 && child.getVisibility() == View.VISIBLE) {
            child.setVisibility(View.GONE);
        } else if (dy <= 0 && child.getVisibility() == View.GONE) {
            child.setVisibility(View.VISIBLE);
        }
    }
}
